package com.smartmedi.ai.controller;

import com.smartmedi.ai.service.FileCollectionService;
import com.smartmedi.ai.service.FileCollectionService.TaskStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件采集控制器
 */
@RestController
@RequestMapping("/ai/file")
@Slf4j
public class FileCollectionController {

    @Autowired
    private FileCollectionService fileCollectionService;

    /**
     * 开始FTP文件采集
     * @param params 采集参数
     * @return 采集任务ID
     */
    @PostMapping("/collect-ftp")
    public ResponseEntity<Map<String, Object>> collectFilesFromFtp(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 参数校验
            if (params == null || !params.containsKey("ftpConfig")) {
                result.put("code", 400);
                result.put("msg", "参数错误：缺少FTP配置信息");
                return ResponseEntity.ok(result);
            }
            
            // 开始采集任务
            String taskId = fileCollectionService.startFtpCollection(params);
            
            result.put("code", 0);
            result.put("msg", "采集任务已启动");
            result.put("data", taskId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("启动FTP采集任务失败", e);
            result.put("code", 500);
            result.put("msg", "启动FTP采集任务失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 开始FTP文件采集
     * @param params 采集参数
     * @return 采集任务ID
     */
    @PostMapping("/collect-ftp/local")
    public ResponseEntity<Map<String, Object>> collectFilesFromFtpinLocal(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 参数校验
            if (params == null || !params.containsKey("ftpConfig")) {
                result.put("code", 400);
                result.put("msg", "参数错误：缺少FTP配置信息");
                return ResponseEntity.ok(result);
            }

            // 开始采集任务
            String taskId = fileCollectionService.startFtpCollection(params);

            result.put("code", 0);
            result.put("msg", "采集任务已启动");
            result.put("data", taskId);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("启动FTP采集任务失败", e);
            result.put("code", 500);
            result.put("msg", "启动FTP采集任务失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 获取采集任务状态
     * @param taskId 任务ID
     * @return 任务状态
     */
    @GetMapping("/collect-status/{taskId}")
    public ResponseEntity<Map<String, Object>> getCollectStatus(@PathVariable String taskId) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            if (taskId == null || taskId.isEmpty()) {
                result.put("code", 400);
                result.put("msg", "参数错误：缺少任务ID");
                return ResponseEntity.ok(result);
            }
            
            TaskStatus status = fileCollectionService.getTaskStatus(taskId);
            
            result.put("code", 0);
            result.put("msg", "获取任务状态成功");
            result.put("data", status);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("获取采集任务状态失败", e);
            result.put("code", 500);
            result.put("msg", "获取采集任务状态失败：" + e.getMessage());
            return ResponseEntity.ok(result);
        }
    }
} 